package com.example.demo.practice.area;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.demo.practice.R;
import com.example.demo.practice.tool.ServerHttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Declan_Wang on 2018/3/12.
 */

public class Street extends Fragment{

    public ServerHttp serverHttp;
    private ArrayList<String> groupData;
    private ArrayList<String> compareData;
    private ArrayList<String> resultData;
    private ArrayList<String> charactors;
    private ListView street;
    private TextView areas;
    private String nameArea;
    private Character character;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_street, null);
    }

    @Override
    public void onAttach(Activity activity) {
// TODO Auto-generated method stub
        super.onAttach(activity);
        if (activity instanceof AreaLists) {
            AreaLists mainActivity = (AreaLists) activity;
            areas = (TextView) mainActivity.findViewById(R.id.areas);//获取父activity的textview控件
            nameArea = areas.getText().toString();
            serverHttp = new ServerHttp(handler);
            serverHttp.setUrl("district");
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        street = (ListView) getActivity().findViewById(R.id.street);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            JSONArray jsonArray = serverHttp.getData();
            groupData = new ArrayList<String>();
            compareData = new ArrayList<>();
            resultData = new ArrayList<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    String adName = json.getString("adname");
                    if (adName.equals(nameArea)) {
                        String name = json.getString("name");
                        groupData.add(name);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            Compare();

            ArrayAdapter<String> Adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item1,resultData);
            street.setAdapter(Adapter);
            serverHttp.clear();
            super.handleMessage(msg);
        }
    };

    /*
     * 汉字转化为首字母并且进行排序
     */
    private void Compare(){
        GetInitials getInitials = new GetInitials();
        for (int i = 0;i < groupData.size();i++){
            compareData.add(getInitials.initials(String.valueOf(groupData.get(i).charAt(0))));
        }
        character = 'A';
        for (int i = 0;i < 26;i++){
            int n = 0;
            for (int j = 0;j < compareData.size();j++){
                if (compareData.get(j).trim().equals(String.valueOf(character))){
                    if (n == 0){
                        resultData.add(String.valueOf(character) + "         " + groupData.get(j));
                        n ++;
                    }else{
                        resultData.add("           " + groupData.get(j));
                    }
                }
            }
            character ++;
        }
    }
}
